package com.ruoyi.api.controller;

import com.ruoyi.result.ResultEnum;
import com.ruoyi.result.ResultView;
import com.ruoyi.system.cache.user.UserCacheService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: lvaitly
 * Time: 15:17
 * Desc: 岗位相关
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
  @Autowired
  private ISysUserService sysUserService;
  @Autowired
  private UserCacheService userCacheService;

  @Value("${sysCfg.adminRoleIds}")
  private List<Long> managerRoleIds;

  /**
   * @param userId
   * @return 返回指定用户的上级以及管理角色用户
   */
  @PostMapping("/list/managers/{userId}")
  private ResultView getManagers(@PathVariable String userId) {
    SysUser parent = userCacheService.getParent(Long.parseLong(userId));
    List<SysUser> sysUsersManagers = sysUserService.selectUsersByRoleIds(managerRoleIds.toArray(new Long[1]));
    Map<String, Object> result = new HashMap<>();
    result.put("parent", parent);
    result.put("managers", sysUsersManagers);
    return ResultView.ok(result);
  }

  @PostMapping("/info/{userId}")
  private ResultView getUserInfo(@PathVariable Long userId) {
    if(userId>0){
      SysUser user=sysUserService.selectUserById(userId);
      if(null!=user){
        List<SysPost> posts=user.getPosts();
        if(null!=posts&&!posts.isEmpty()){

        }
      }
    }
    return ResultView.ok(sysUserService.selectUserById(userId));
  }

  @PostMapping("/edit/{userId}")
  private ResultView editUserInfo(@RequestBody SysUser user){
    int i = sysUserService.updateUser(user);
    if(i > 0){
      return ResultView.ok();
    }
    return ResultView.error(ResultEnum.CODE_1004);
  }

}
